
var applyFileDialog = {
	dialog: null,
	oThis: null, // 当前弹窗对象
	oSearch: null,
	oHzTbody: null,
	oYxTbody: null,

	hzTable: {
		paging: null,
		current: 1,
		size: 4,
		list: []
	},
	hzInfo: null, // 患者信息

	yxTable: {
		paging: null,
		current: 1,
		size: 4,
		list: []
	},
	yxInfo: null, // 影像信息

	init: function () {
		this.initState();
		this.initEvent();
	},
	initState: function () {
		var _this = this;

		this.dialog = new TheDialog('#yxDialog');
		this.oThis = $('.yx-dialog');
		this.oSearch = this.oThis.find('.search-box');
		this.oHzTbody = this.oThis.find('.table1 .com-table-body tbody');
		this.oYxTbody = this.oThis.find('.table2 .com-table-body tbody');

		// 患者分页
		this.hzTable.paging = new ThePaging('#yxHzPaging', {
			size: this.hzTable.size,
			currentChange: function (current) {
				_this.hzTable.current = current;
				_this.getHzList();
			}
		})

		// 影像文件分页
		this.yxTable.paging = new ThePaging('#yxPaging', {
			size: this.yxTable.size,
			currentChange: function (current) {
				_this.yxTable.current = current;
				_this.getYxList();
			}
		})

		// 获取患者列表
		this.getHzList();
	},

	initEvent: function () {
		var _this = this;

		// 点击搜索查询患者信息
		this.oSearch.on('click', '.search-btn', function () {
			_this.hzTable.current = 1;
			_this.getHzList();
		})

		// 患者tr点击-选中患者信息，查询影像信息
		this.oHzTbody.on('click', 'tr', function () {
			var oTr = $(this);
			var idx = oTr.attr('idx');
			_this.hzInfo = _this.hzTable.list[idx];
			_this.getYxList();
		})


		// 影像tr点击-选中影像信息
		this.oYxTbody.on('click', 'tr', function () {
			var oTr = $(this);
			var idx = oTr.attr('idx');
			if (oTr.hasClass('empty')) {
				return;
			}
			_this.yxInfo = _this.yxTable.list[idx];
			_this.oYxTbody.find('td:last-child').html('');
			oTr.find('td:last-child').html('<i class="icon iconfont icongou"></i>');
		})

		this.oThis.find('.submit-btn').on('click', function () {
			if (!_this.yxInfo) {
				pb.confirm('请选择影像信息!');
			} else {
				oApplyDetailDialog.setHzYx(_this.hzInfo, _this.yxInfo);
				_this.dialog.close();
			}
		})
	},

	show: function () {
		this.dialog.show();
	},

	// 获取患者列表
	getHzList: function () {
		var _this = this;
		$.ajax({
			type: 'post',
			url: pb.api + '/baPatient/listData',
			data: {
				applyNo: this.oSearch.find('.applyNo').val(),
				patientNameSpell: this.oSearch.find('.patientNameSpell').val(),
				inspectionCode: this.oSearch.find('.inspectionCode').val(),
				imageCode: this.oSearch.find('.imageCode').val(),
				outpatientCode: this.oSearch.find('.outpatientCode').val(),
				hospitalizedCode: this.oSearch.find('.hospitalizedCode').val(),
				patientName: this.oSearch.find('.patientName').val(),
				pageNo: this.hzTable.current,
				pageSize: this.hzTable.size
			},
			success: function (res) {
				var list = _this.formatHz(res.list);
				_this.hzTable.list = list;
				_this.setHzTbody(list);
				_this.hzTable.paging.setTotal(res.count);
				// 获取影像列表
				if (list.length > 0) {
					_this.hzInfo = list[0];
					_this.yxTable.current = 1;
					_this.getYxList();
				}
			}
		})
	},
	formatHz: function (list) {
		var _this = this;
		list.forEach(function (item, index) {
			for (var key in item) {
				item[key] = item[key] || '-';
			}
			item.xh = (_this.hzTable.current-1)*_this.hzTable.size+index+1;
		})
		return list;
	},
	setHzTbody: function (list) {
		var _this = this;
		var html = '';
		if (list.length > 0) {
			list.forEach(function (item, index) {
				html += '<tr idx="' + index + '">' +
							'<td>' + item.xh + '</td>' +
							'<td title="' + (item.patientName || '') + '">' + (item.patientName || '') + '</td>' +
							'<td title="' + (item.sex || '') + '">' + (item.sex || '') + '</td>' +
							'<td title="' + (item.age || '') + '">' + (item.age || '') + '岁</td>' +
							'<td title="' + (item.patientType || '') + '">' + (item.patientType || '') + '</td>' +
							'<td title="' + (item.checkPointMethod || '') + '">' + (item.checkPointMethod || '') + '</td>' +
							'<td title="' + (item.inspectionCode || '') + '">' + (item.inspectionCode || '') + '</td>' +
							'<td title="' + (item.imageCode || '') + '">' + (item.imageCode || '') + '</td>' +
							'<td title="' + (item.outpatientCode || '') + '">' + (item.outpatientCode || '') + '</td>' +
							'<td title="' + (item.hospitalizedCode || '') + '">' + (item.hospitalizedCode || '') + '</td>' +
							'<td title="' + (item.equipmentDetail || '') + '">' + (item.equipmentDetail || '') + '</td>' +
						'</tr>';
			})
			
		} else {
			html = '<tr class="empty"><td>暂无数据</td></tr>';
		}
		this.oThis.find('.table1 .com-table-body tbody').html(html);
	},

	// 获取影像信息
	getYxList: function () {
		var _this = this;
		$.ajax({
			type: 'get',
			url: pb.api + '/baPatient/imageListData',
			data: {
				patientId: this.hzInfo.id,
				pageNo: this.yxTable.current,
				pageSize: this.yxTable.size
			},
			success: function (res) {
				var list = _this.formatYx(res.list);
				_this.yxTable.list = list;
				_this.yxTable.paging.setTotal(res.count);
				_this.yxInfo = null;
				_this.setYxTbody(list);
			}
		})
	},
	formatYx: function (list) {
		var _this = this;
		list.forEach(function (item, index) {
			for (var key in item) {
				item[key] = item[key] || '-';
			}
		})
		return list;
	},
	setYxTbody: function (list) {
		var _this = this;
		var html = '';
		if (list.length > 0) {
			list.forEach(function (item, index) {
				html += '<tr idx="' + index + '">' +
							'<td>' + item.seriaiuid + '</td>' +
							'<td>' + item.imageName + '</td>' +
							'<td>' + item.sort + '</td>' +
							'<td>' + item.sequenceMark + '</td>' +
							'<td>' + item.imageNuber + '</td>' +
							'<td></td>' +
						'</tr>'
			})
		} else {
			html = '<tr class="empty"><td>暂无数据</td></tr>';
		}
		this.oThis.find('.table2 .com-table-body tbody').html(html);
	},


}

$(function () {
	applyFileDialog.init();
})

